package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.interceptor.JwtTokenUserInterceptor;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.properties.JwtProperties;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart =new ShoppingCart();
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //判断此菜品/套餐是否已存在购物车：存在——update数量 不存在——insert数据
        Integer shoppingCartId = shoppingCartMapper.exist(shoppingCart);
        if(shoppingCartId!=null){
            //存在——增加菜品/套餐数量
            shoppingCartMapper.addNums(shoppingCartId);
        }else {
            //不存在——新增菜品数据
            if(shoppingCartDTO.getDishId()!=null){
                DishVO dishData = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setImage(dishData.getImage());
                shoppingCart.setName(dishData.getName());
                shoppingCart.setAmount(dishData.getPrice());
            }else { //不存在——新增套餐数据
                Setmeal setmealData = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setImage(setmealData.getImage());
                shoppingCart.setName(setmealData.getName());
                shoppingCart.setAmount(setmealData.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.list(userId);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart =new ShoppingCart();
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //查找此商品对应id
        shoppingCart= shoppingCartMapper.select(shoppingCart);
        //数量为1，删除此商品
        if(shoppingCart.getNumber()==1){
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }else {
            //数量大于1，减少1个数量
            shoppingCartMapper.subNums(shoppingCart.getId());
        }
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

}
